package com.lyf.shop.dao.Impl;

import com.lyf.shop.dao.OrderDao;
import com.lyf.shop.entity.*;
import com.lyf.shop.util.DataBaseUtil;
import com.lyf.shop.util.StrUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import javax.xml.crypto.Data;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderDaoImpl implements OrderDao {
    private QueryRunner qr = new QueryRunner();
    @Override
    public int InsertOrder(Orders orders) throws SQLException {
        return qr.update(DataBaseUtil.getConnection(),"insert into orders values(?,?,?,?,?,?)",
                orders.getOid(), orders.getUid(), orders.getAid(),orders.getOcount(),orders.getOtime(),orders.getOstate());
    }

    @Override
    public void insertItem(List<Item> items) throws SQLException {
        Object[][] param = new Object[items.size()][];
        //循环遍历
        for (int i =0;i<items.size();i++){
            Item item = items.get(i);
            param[i] = new Object[]{item.getOid(),item.getPid(),item.getIcount(),item.getInum()};
        }
        //批量新增
        qr.batch(DataBaseUtil.getConnection(),"insert into item values (default,?,?,?,?)", param);
    }

    @Override
    public List<Orders> GetOrderList(int uid) throws SQLException {
        String sql = "select o.oid,o.uid,o.ocount,o.otime,o.ostate,a.aid,a.aname,a.aphone,a.adetail,a.astate" +
                " from orders o,address a where a.aid=o.aid and o.uid=?";
        List<Map<String, Object>> query = qr.query(DataBaseUtil.getConnection(), sql, new MapListHandler(), uid);
        if(query==null){
            return null;
        }
        List<Orders> ordersList = new ArrayList<>();
        for (Map<String, Object> map : query) {
            //声明需要数据封装的实体类
            Orders orders = new Orders();
            Address address = new Address();
            //数据封装
            try {
                BeanUtils.populate(orders,map);
                BeanUtils.populate(address,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            orders.setAddr(address);
            ordersList.add(orders);
        }
        return ordersList;
    }

    @Override
    public List<Item> getItemByOid(String oid) throws SQLException  {
        String sql = "select i.iid,i.oid,i.icount,i.inum,p.pid,p.pname,p.ptime,p.pimage,p.pprice,p.pstate,p.pinfo " +
                "from item i,product p where i.pid=p.pid and i.oid=?";
        List<Map<String, Object>> query = qr.query(DataBaseUtil.getConnection(), sql, new MapListHandler(), oid);
        if (query==null) {
            return null;
        }

        List<Item> itemList = new ArrayList<>();

        for (Map<String, Object> map : query) {
            //声明需要封装的实体类
            Item i = new Item();
            Product product = new Product();
            try {
                BeanUtils.populate(i,map);
                BeanUtils.populate(product,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            i.setPro(product);
            itemList.add(i);
        }
        return itemList;
    }

    @Override
    public Orders getOrderByOid(String oid) throws SQLException{
        String sql = "select o.oid,o.uid,o.ocount,o.otime,o.ostate,a.aid,a.aname,a.aphone,a.adetail,a.astate" +
                " from orders o,address a where a.aid=o.aid and o.oid=?";
        Map<String,Object> query = qr.query(DataBaseUtil.getConnection(), sql, new MapHandler(), oid);
        //声明需要封装的实体类
        Orders orders = new Orders();
        Address address = new Address();
        try {
            BeanUtils.populate(orders,query);
            BeanUtils.populate(address,query);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        orders.setAddr(address);
        return orders;
    }

    @Override
    public int updateOrderStateToPaySuccess(String oid) throws SQLException{
        return qr.update(DataBaseUtil.getConnection(),"update orders set ostate=1 where oid=?",oid);
    }

    @Override
    public List<Orders> getAllOrders(String uname, String ostate) throws SQLException {
        String sql = "select o.oid,o.uid,o.aid,o.ocount,o.otime,o.ostate,u.uname" +
                " from orders o,user u where o.uid=u.uid";
        List<Object> ol = new ArrayList<>();
        if (StrUtils.empty(uname)){
            sql+=" and u.uname like ?";
            ol.add("%"+uname+"%");
        }
        if(StrUtils.empty(ostate)){
            Integer state = Integer.valueOf(ostate);
            sql+=" and ostate = ?";
            ol.add(state);
        }
        List<Map<String, Object>> query = qr.query(DataBaseUtil.getConnection(), sql, new MapListHandler(), ol.toArray());
        if(query==null){
            return null;
        }
        List<Orders> ordersList = new ArrayList<>();
        for (Map<String, Object> map : query) {
            //声明需要封装数据的的实体类
            Orders o = new Orders();
            User u = new User();
            try {
                BeanUtils.populate(o,map);
                BeanUtils.populate(u,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            o.setUser(u);
            ordersList.add(o);
        }
        return ordersList;
    }

    @Override
    public int updateOrderStateToSend(String oid, int state) throws SQLException {
        return qr.update(DataBaseUtil.getConnection(),"update orders set ostate=? where oid=?",state,oid);
    }
}
